//
//  FindViewController.m
//  MXClassroom
//
//  Created by 你个LB on 16/9/8.
//  Copyright © 2016年 你个LB. All rights reserved.
//

#import "DiscoverViewController.h"

@interface DiscoverViewController ()

@end

@implementation DiscoverViewController

- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"scrollView.contentOffset" context:nil];
}

- (void)initRightSearchBtn{
    UIButton * searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.frame = CGRectMake(0, 0, 25, 25);
    [searchBtn setImage:[UIImage imageNamed:@"icon_main_discovery_search.png"] forState:UIControlStateNormal];
    [searchBtn addTarget:self action:@selector(searchBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:searchBtn];
}

- (void)searchBtnAction{
    SearchViewController * searchVC = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initRightSearchBtn];

    // 创建选择按钮视图
    _segmented = [[CXSegmentedBarView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width - 120, 40)];
    _segmented.backgroundColor = [UIColor clearColor];
    _segmented.titleColor = kColor_text_color;
    _segmented.selectedTitleColor = [UIColor blackColor];
    _segmented.selectedViewbgColor = [UIColor blackColor];
    _segmented.titles = @[@"推荐",@"分类",@"榜单",@"大德"];

    // 添加事件
    [_segmented addTarget:self action:@selector(segmentedValueChanged:) forControlEvents:UIControlEventValueChanged];
    [self.navigationItem setTitleView:_segmented];
    // 3.设置视图的内容视图
    // 01 设置滑动内容视图的高度
    self.contentSizeHeight = kScreen_height - 64;
    // 02 设置滑动视图的位置
    self.scrollView.top = 64;
    // 03 设置滑动视图内容视图
    RecommendViewController * recommendVC = [[RecommendViewController alloc] init];

    ClassificationViewController * classificationVC = [[ClassificationViewController alloc] init];

    ListViewController * listVC = [[ListViewController alloc] init];
    
    MoralityViewController * moralityVC = [[MoralityViewController alloc] init];
    
    self.viewControllers = @[recommendVC, classificationVC, listVC, moralityVC];
    // 4.添加观察者模式
    [self addObserver:self forKeyPath:@"scrollView.contentOffset" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 选项按钮事件
- (void)segmentedValueChanged:(CXSegmentedBarView *)segmented
{
    self.selectedIndex = segmented.selectedIndex;
}

#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    
    if ([keyPath isEqualToString:@"scrollView.contentOffset"]) {
        // 1.获取所监听到的内容
        id value = change[@"new"];
        // 2.获取当前滑动视图水平方向的偏移量
        CGFloat scrollView_contentOffset_x = [value CGPointValue].x;
        // 3.获取当前滑动视图的页数索引
        NSInteger pageIndex = (int)scrollView_contentOffset_x / (int)kScreen_width;
        // 获取当前页面视图的偏移宽度
        float p_width = scrollView_contentOffset_x - pageIndex * kScreen_width;
        // 4.获取当前页面的偏移比例
        float scale = p_width / kScreen_width;
        // 5.设置选中视图的位置
        _segmented.selectedView.left = pageIndex * _segmented.selectedView.width + _segmented.selectedView.width * scale;
    }
}

#pragma mark - 监听当前选中页面的索引位置
- (void)setSelectedIndex:(NSInteger)selectedIndex
{
    [super setSelectedIndex:selectedIndex];
    
    // 设置选项按钮选中的索引位置
    _segmented.selectedIndex = self.selectedIndex;
}

@end
